package com.doeis.uwork.dao;

import java.util.ArrayList;
import java.util.List;

import com.doeis.uwork.entity.HnMsgAuthority;
import com.google.inject.Singleton;
import com.voland.core.db.Dao;
import com.voland.core.db.DaoException;
import com.voland.core.db.Page;

@Singleton
public class HnMsgAuthorityDao extends Dao {

	public HnMsgAuthority findHnMsgAuthorityByid(Object id) throws DaoException{
		try{
			return super.readById(HnMsgAuthority.class, id);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}

	public Page<HnMsgAuthority> listHnMsgAuthoritys(Page page, HnMsgAuthority hnMsgAuthority) throws DaoException{
		try{
			 String sql = "select * from hn_msg_authority";
			return super.querySlice(HnMsgAuthority.class, sql, page);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	public List<String> listSubHnMsgAuthorityLevels(String prarentLevel, String switchboardid) throws DaoException{
		try{
			String sql = "select level from hn_msg_authority where switchboard_id = ? and level like ? ";
			return super.query(String.class, sql, switchboardid, prarentLevel+"%");
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	public List<HnMsgAuthority> listSubHnMsgAuthority(String prarentLevel, String switchboardid,Boolean status) throws DaoException{
		try{
			StringBuffer sql = new StringBuffer("select * from hn_msg_authority where  switchboard_id = ? and  "
					+ "level like ? ");
			if(status != null){
				sql.append(" and status =?");
			}
			return super.query(HnMsgAuthority.class, sql.toString(), switchboardid, prarentLevel+"__%",status);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	/**
	 * 查出所有红牛权限
	 * @return
	 * @throws DaoException
	 */
	public List<HnMsgAuthority> listHnMsgAuthoritys(HnMsgAuthority hnMsgAuthority) throws DaoException{
		try{
			List<Object> params = new ArrayList<Object>();
			StringBuffer sql = new StringBuffer("select * from hn_msg_authority where switchboard_id = ? ");
			params.add( hnMsgAuthority.getSwitchboard_id() );
			if(hnMsgAuthority != null){
				Boolean status = hnMsgAuthority.getStatus();
				if(status != null){
					sql.append(" and status = ? ");
					params.add(status);
				}
				if (hnMsgAuthority.getMsg_user_id()!=null) {
					sql.append(" and msg_user_id = ? ");
					params.add(hnMsgAuthority.getMsg_user_id());
				}
			}
			sql.append("  order by level ");
			
			return super.query(HnMsgAuthority.class, sql.toString(), params.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}

}